<template>
  <div class="container">
    <el-form
      :inline="true"
      :model="partnerform1"
      ref="partnerform1"
      class="demo-form-inline"
    >
      <el-form-item label="策略搜索">
        <el-input v-model="partnerform1.name" clearable placeholder="请输入" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button
          @click="newAndChange('新建')"
          size="mini"
          icon="el-icon-circle-plus-outline"
          style="background-color: #ff8437; color: #fff"
          >新建</el-button
        >
      </div>
      <el-table :data="policyList">
        <!-- 放置列 -->
        <el-table-column
          prop="index"
          align="center"
          width="50px"
          label="序号"
        />
        <el-table-column prop="policyName" align="start" label="策略名称" />
        <el-table-column prop="discount" align="start" label="策略方案" />
        <el-table-column  align="start" label="创建时间">
          <template v-slot="{ row }">
            {{ row.createTime.replace("T", " ") }}
          </template>
        </el-table-column>
        <el-table-column align="center" width="200px" label="操作">
          <!-- 放置操作按钮 -->
          <template v-slot="{ row }">
             <el-button
              size="mini"
              type="text"
              @click="details(row)"
              >查看详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="newAndChange('修改', row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              @click="del(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row
        v-if="this.totalpage > 1"
        type="flex"
        style="height: 60px"
        align="middle"
        justify="space-between"
      >
        <el-col :span="12"
          ><span style="font-size: 13px; color: #999; padding-left: 12px"
            >共{{ totalcount }}条记录 第{{ pageParams.pageIndex }}/{{
              totalpage
            }}页</span
          ></el-col
        >
        <el-col :push="12">
          <el-button-group>
            <el-button
              type="primary"
              @click="innPage"
              size="mini"
              :disabled="pageParams.pageIndex == 1"
              icon="el-icon-arrow-left"
              style="
                background-color: #d5ddf8;
                color: #000;
                margin-right: 12px;
                border: none;
              "
              >上一页</el-button
            >
            <el-button
              type="primary"
              @click="addPage"
              size="mini"
              :disabled="pageParams.pageIndex == this.totalpage"
              style="background-color: #d5ddf8; color: #000; border: none"
              >下一页<i class="el-icon-arrow-right el-icon--right"></i
            ></el-button>
          </el-button-group>
        </el-col>
      </el-row>

      <!-- 新增合作商的dialog -->
      <el-dialog
        :title="models"
        :visible.sync="dialogFormVisible"
        @close="closefn"
      >
        <el-form
          :model="addPartnerForm"
          :rules="rules"
          label-width="120px"
          ref="partnerform"
        >
          <el-form-item label="策略名称" prop="policyName">
            <el-input
              v-model="addPartnerForm.policyName"
              maxlength="15"
              show-word-limit
              style="width: 80%"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="策略方案" prop="discount">
            <el-input-number
              v-model="addPartnerForm.discount"
              controls-position="right"
              placeholder="请输入"
              style="width: 80%; display: flex; flex-direction: column"
            >
            </el-input-number>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="quitfn">取 消</el-button>
          <el-button type="primary" @click="submitfn">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 策略详情的dialog -->
    <el-dialog
      title="策略详情"
      :visible="showPolicytails"
      @close="showPolicytails=false"
    >
      <div>
        <div class="single"><span class="title">策略名称:</span><span class="right" style="margin-left:30px">{{policyName}}</span></div>
        <div class="single"><span class="title">策略方案:</span><span class="right">
          <el-table :data="nodelist">
            <el-table-column prop="index" align="center" width="50px" label="序号"></el-table-column>
            <el-table-column prop="nodeName" align="center" label="点位名称"></el-table-column>
            <el-table-column prop="innerCode" align="center" label="设备编号"></el-table-column>
          </el-table>
          </span></div>
      </div>
       <!-- 放置分页组件 -->
      <el-row
        v-if="this.totalpage1 > 1"
        type="flex"
        style="margin-top: 20px"
        align="middle"
        justify="space-between"
      >
        <el-col :span="12"
          ><span style="font-size: 13px; color: #999; padding-left: 12px"
            >共{{ totalcount1 }}条记录 第{{ pageParams1.pageIndex }}/{{
              totalpage1
            }}页</span
          ></el-col
        >
        <el-col :push="6">
          <el-button-group>
            <el-button
              type="primary"
              @click="innPage1"
              size="mini"
              :disabled="pageParams1.pageIndex == 1"
              icon="el-icon-arrow-left"
              style="
                background-color: #d5ddf8;
                color: #000;
                margin-right: 12px;
                border: none;
              "
              >上一页</el-button
            >
            <el-button
              type="primary"
              @click="addPage1"
              size="mini"
              :disabled="pageParams1.pageIndex == this.totalpage1"
              style="background-color: #d5ddf8; color: #000; border: none"
              >下一页<i class="el-icon-arrow-right el-icon--right"></i
            ></el-button>
          </el-button-group>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import {
  getpolicylist,
  addpolicy,
  changePolicy,
  getpolicydetail,
  delpolicy,
} from "@/api/policy";
export default {
  name: "Node",
  components: {},
  data() {
    return {
      policyName:'',
      models: "新建",
      showPolicytails:false,
      dialogFormVisible: false,
      totalcount: "",
      totalpage: "",
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
      },
      totalcount1: "",
      totalpage1: "",
      pageParams1: {
          pageIndex: 1,
          pageSize: 10,
        },
      addPartnerForm: {
        policyName: "",
        discount: "",
      },
      partnerform1: {
        name: "",
      },
      nodelist:[],
      policyList: [],
      rules: {
        policyName: [{ required: true, message: "请输入商品名称" }],
        discount: [{ required: true, message: "请输入策略方案" }],
      },
    };
  },
  async created() {
    this.getpolicylist();
  },
  methods: {
    //下一页
    addPage() {
      this.pageParams.pageIndex++;
      this.getpolicylist();
    },
    // 上一页
    innPage() {
      this.pageParams.pageIndex--;
      this.getpolicylist();
    },
    //下一页
    addPage1() {
      this.pageParams1.pageIndex++;
      this.getpolicylist();
    },
    // 上一页
    innPage1() {
      this.pageParams1.pageIndex--;
      this.getpolicylist();
    },
   async details(row){
    this.policyName=row.policyName
      const res = await getpolicydetail(
        this.pageParams1.pageIndex,
        this.pageParams1.pageSize,
        row.policyId
      );
      this.nodelist = res.currentPageRecords;
      this.totalpage1 = res.totalPage;
      this.totalcount1 = res.totalCount;
      this.nodelist.forEach((item, index) => {
        this.$set(
          item,
          "index",
          this.pageParams1.pageSize * (this.pageParams1.pageIndex - 1) + index + 1
        );
      });
      this.showPolicytails=true
    },
    async getpolicylist() {
      const res = await getpolicylist(
        this.pageParams.pageIndex,
        this.pageParams.pageSize,
        this.partnerform1.name
      );
      this.policyList = res.currentPageRecords;
      this.totalpage = res.totalPage;
      this.totalcount = res.totalCount;
      this.policyList.forEach((item, index) => {
        this.$set(
          item,
          "index",
          this.pageParams.pageSize * (this.pageParams.pageIndex - 1) + index + 1
        );
      });
    },
    search() {
      this.getpolicylist();
    },
    closefn() {
      this.$refs.partnerform.resetFields();
      (this.addPartnerForm = {
        policyName: "",
        discount: "",
      }),
        (this.dialogFormVisible = false);
    },
    quitfn() {
      this.closefn();
    },
    submitfn() {
      this.$refs.partnerform.validate(async (right) => {
        if (right) {
          if (!this.addPartnerForm.policyId) {
            await addpolicy(this.addPartnerForm);
            this.$message.success("添加成功");
            this.pageParams.pageIndex = 1;
            this.getpolicylist();
            this.closefn();
          } else {
            const res = await changePolicy(
              this.addPartnerForm
            );
            if (res == true) {
              this.$message.success("修改成功");
              this.closefn();
              this.getpolicylist();
            }
          }
        } else {
          this.$message.error(new Error());
        }
      });
    },
    //新建and修改
    async newAndChange(model, row) {
      if (model == "新建") {
        this.models = "新增策略";
        this.dialogFormVisible = true;
      } else {
        this.models = "修改策略";
        console.log(row);
        this.addPartnerForm.policyName = row.policyName;
        this.addPartnerForm.discount = row.discount;
        this.addPartnerForm.policyId = row.policyId;
        this.dialogFormVisible = true;
      }
    },
    async del(row) {
      this.$confirm("确认删除？").then(async () => {
        await delpolicy(row.policyId);
        if (this.policyList.length === 1) {
          this.pageParams.pageIndex--;
        }
        this.getpolicylist();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.role-operate {
  padding: 10px;
}
::v-deep .el-dialog {
  border-radius: 10px;
  padding: 20px;
  width: 50%;
  .dialog-footer {
    margin-top: -30px;
  }
  .area {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
  }
  .title {
    width: 50%;
    margin-bottom: 20px;
    .right {
      width: 80%;
      margin-left: 10px;
      .el-table {
        margin-top: -15px;
      }
    }
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #dd2626;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.avatar {
  width: 50px;
  height: 50px;
  display: block;
}
</style>
